import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)
import login from '../views/user/login.vue'
import home from '../views/home/home.vue'
import movie from '../views/movie/movie.vue'
import qa from '../views/qa/qa.vue'
import user from '../views/user/user.vue'
import layout from '../views/layout.vue'
import notfound from '../views/notfound/notfound.vue'
import edit from '../views/user/edit.vue'
const routes = [
  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path: '/edit',
    name: 'edit',
    component: edit
  },
  {
    //二级路由
    path: '/',
    name: 'layout',
    component: layout,
    children: [
      {
        path: '/home',
        name: 'home',
        component: home
      },
      {
        path: '/movie',
        name: 'movie',
        component: movie
      },
      {
        path: '/qa',
        name: 'qa',
        component: qa
      },
      {
        path: '/user',
        name: 'user',
        component: user,
        meta: { isLogin: true }
      },
      {
        path: '/',
        //重定向
        redirect: '/home'
      }
    ]
  },
  {
    path: '/notfound',
    name: 'notfound',
    component: notfound
  },
  {
    path: '*',
    //重定向
    redirect: 'notfound'
  }
]

const router = new VueRouter({
  routes
})
import {gettoKen} from '../utils/token'
//导航守卫判断用户有没有登录
router.beforeEach((to,from,next)=>{
  if(!gettoKen()&&to.meta.isLogin){
    next({path:'/login'})
  }else{
    next()
  }
})
export default router
